package com.gxgeek.hello;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author gaoxing
 * @create 2018-05-16 上午1:16
 */
public class Customer {

    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) throws Exception {
        //步骤一：建立连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //步骤二：创建一个消费者对象，并利用handleDelivery回调函数接受消息
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("[Message Recv] => " + msg);
            }
        };
        //步骤三：将消费者端与消息队列绑定
        channel.basicConsume(QUEUE_NAME, false, consumer);
    }

}
